layui
    .config({
        base: '../layui/exts/', //静态资源所在路径
    })
    .use(['table', 'form', 'util'], function () {
        var $ = layui.$;
        var form = layui.form;
        var util = layui.util;
        var table = layui.table;

        openUser();

        function openUser() {
            let values = {};
            table.render({
                elem: '#dataTable'
                , url: '/system/getUserData'
                , contentType: "application/json"
                , method: 'post'
                , cellMinWidth: 40
                , page: true
                , limit: 50
                , height: 'full-65'
                , loading: true
                , where: values
                , size: 'sm'
                , cols: [
                    [
                        {field: 'username', title: '用户账号', fixed: 'left', width: 80},
                        {field: 'realName', title: '姓名', fixed: 'left', width: 120},
                        {field: 'enabled', title: '激活', fixed: 'left', width: 40, templet: "#enabledTpl"},
                        {field: 'groupName', title: '用户组', width: 80},
                        {field: 'employer', title: '所属单位', width: 250},
                        {field: 'ip', title: '登录IP', width: 120},
                        {field: 'phoneNumber', title: '联系电话', width: 100},
                        {field: 'mobileNumber', title: '手机号', width: 100},
                        {field: 'email', title: '电子邮箱', width: 100},
                        {field: 'createor', title: '创建人', width: 80},
                        {field: 'createTime', title: '创建时间', width: 120},
                        {field: 'updater', title: '最后更新', width: 80},
                        {field: 'updateTime', title: '更新时间', width: 120},
                        {field: 'lastLoginTime', title: '最后登录时间', width: 120},
                        {field: 'lastLoginIp', title: '最后登录IP', width: 100},
                        {field: 'remark', title: '备注'},
                        {fixed: 'right', title: '操作', width: 140, toolbar: '#operationBar'}
                    ]
                ],
            });
        }

        //监听工具条
        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case "edit":
                    top.layer.open({
                        type: 2,
                        title: '编辑用户信息',
                        content: './system/setUserInfo?id=' + data.id,
                        area: ['628px', '480px'],
                        shadeClose: false,
                        resize: false,
                        btn: [
                            '<i class="layui-icon">&#xe605;</i> 确定',
                            '<i class="layui-icon">&#x1006;</i> 取消',
                        ],
                        yes: function (index, layero) {
                            var iframeWin = layero.find('iframe')[0];
                            if (iframeWin.contentWindow.Submit()) {
                                table.reload('dataTable');
                                layer.close(index);
                            }
                            return false;
                        },
                        btn2: function (index) {
                            layer.close(index);
                        },
                    });
                    return false;
                case 'del':
                    top.layer.confirm('确定要删除当前选择的用户信息吗？ ', function (index) {
                        $.ajax({
                            url: '../system/delUserInfo', //url
                            dataType: 'json', //数据格式
                            type: 'POST', //请求方式
                            async: false, //是否异步请求
                            data: {id: data.id},
                            success: function (result) {
                                if (result.code === 0) {
                                    obj.del();
                                    layer.close(index);
                                    top.parent.layer.msg(result.message, {icon: 1});
                                } else {
                                    top.parent.layer.alert(result.message, {icon: 2});
                                }
                            },
                        });
                    });
                    return false;
                case "activated":
                    var info = "确定要激活用户：" + data.username + "(" + data.realName + ")" + ' 吗？';
                    top.layer.confirm(info, function (index) {
                        $.ajax({
                            url: './setUserEnabled', //url
                            contentType: "Application/json",
                            dataType: 'json', //数据格式
                            type: 'POST', //请求方式
                            async: false, //是否异步请求
                            data: JSON.stringify({id: data.id, action: "1"}),
                            success: function (result) {
                                if (result.code === 0) {
                                    layer.close(index);
                                    table.reload('dataTable');
                                    top.parent.layer.msg(result.message, {icon: 1});
                                } else {
                                    top.parent.layer.alert(result.message, {icon: 2});
                                }
                            },
                        });
                    });
                    return false;
                case "disabled":
                    var info = "确定要暂停用户：" + data.username + "(" + data.realName + ")" + ' 使用吗？';
                    top.layer.confirm(info, function (index) {
                        $.ajax({
                            url: './setUserEnabled', //url
                            contentType: "Application/json",
                            dataType: 'json', //数据格式
                            type: 'POST', //请求方式
                            async: false, //是否异步请求
                            data: JSON.stringify({id: data.id, action: "0"}),
                            success: function (result) {
                                if (result.code === 0) {
                                    layer.close(index);
                                    table.reload('dataTable');
                                    top.parent.layer.msg(result.message, {icon: 1});
                                } else {
                                    top.parent.layer.alert(result.message, {icon: 2});
                                }
                            },
                        });
                    });
                    return false;
                case "restPwd":
                    top.layer.confirm("确定要重置" + data.username + "(" + data.realName + ")" + "的密码吗？", function (index) {
                        $.ajax({
                            url: './resetPassword', //url
                            contentType: "Application/json",
                            dataType: 'json', //数据格式
                            type: 'POST', //请求方式
                            async: false, //是否异步请求
                            data: JSON.stringify({id: data.id}),
                            success: function (result) {
                                if (result.code == 0) {
                                    top.parent.layer.open({
                                        icon: 1,
                                        title: '提示信息',
                                        content: result.message
                                    });
                                    layer.close(index);
                                } else {
                                    top.parent.layer.alert(result.message, {icon: 2});
                                }
                            },
                        });
                    });
                    return false;
                case "userRole":
                    top.layer.open({
                        type: 2,
                        title: '设置用户角色',
                        content: './system/setUserRole?userId=' + data.id,
                        area: ['520px', '550px'],
                        shadeClose: false,
                        resize: false,
                    });
                    return false;
                case "userTask":
                    top.layer.open({
                        type: 2,
                        title: '设置用户任务',
                        content: './project/setUserTask?userId=' + data.id,
                        area: ['520px', '550px'],
                        shadeClose: false,
                        resize: false,
                    });
                    return false;
                case "userWorkProcedure":
                    top.layer.open({
                        type: 2,
                        title: '设置用户工序',
                        content: './project/setUserWorkProcedure?userId=' + data.id,
                        area: ['520px', '600px'],
                        shadeClose: false,
                        resize: false,
                    });
                    return false;
            }
        });

        util.on("lay-event", {
            newUser: function () {
                top.layer.open({
                    type: 2,
                    title: '新建用户',
                    content: './system/setUserInfo',
                    area: ['628px', '480px'],
                    shadeClose: false,
                    resize: false,
                    btn: [
                        '<i class="layui-icon">&#xe605;</i> 确定',
                        '<i class="layui-icon">&#x1006;</i> 取消',
                    ],
                    yes: function (index, layero) {
                        var iframeWin = layero.find('iframe')[0];
                        if (iframeWin.contentWindow.Submit()) {
                            table.reload('dataTable');
                            layer.close(index);
                        }
                        return false;
                    },
                    btn2: function (index) {
                        layer.close(index);
                    },
                });
            }

        });

    });